using Lzfy_His_Service.Models;
using Lzfy_His_Service.Services;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Lzfy_His_Service.Controllers
{
    /// <summary>
    /// 在院病人查询控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class InpatientController : ControllerBase
    {
        private readonly IInpatientService _inpatientService;
        private readonly ILogger<InpatientController> _logger;

        public InpatientController(
            IInpatientService inpatientService,
            ILogger<InpatientController> logger)
        {
            _inpatientService = inpatientService;
            _logger = logger;
        }

        /// <summary>
        /// 查询在院病人列表
        /// </summary>
        /// <param name="request">查询请求</param>
        /// <returns>在院病人列表</returns>
        [HttpPost("list")]
        public async Task<IActionResult> GetInpatientList([FromBody] InpatientListRequest request)
        {
            try
            {
                // 参数验证
                if (request == null || string.IsNullOrWhiteSpace(request.CurrentWard))
                {
                    return BadRequest(new InpatientListResponse
                    {
                        Success = false,
                        Message = "科室代码不能为空",
                        Data = new List<InpatientInfo>(),
                        TotalCount = 0
                    });
                }

                // 设置默认医院区域
                if (string.IsNullOrWhiteSpace(request.HospitalArea))
                {
                    request.HospitalArea = "LZ001";
                }

                // 查询在院病人列表
                var inpatientList = await _inpatientService.GetInpatientListAsync(request);

                _logger.LogInformation("在院病人列表查询成功，科室代码: {CurrentWard}, 医院区域: {HospitalArea}，记录数: {Count}", 
                    request.CurrentWard, request.HospitalArea, inpatientList.Count);

                return Ok(new InpatientListResponse
                {
                    Success = true,
                    Message = inpatientList.Count > 0 ? "查询成功" : "该科室暂无在院病人",
                    Data = inpatientList,
                    TotalCount = inpatientList.Count
                });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "在院病人列表查询失败，科室代码: {CurrentWard}, 医院区域: {HospitalArea}", 
                    request?.CurrentWard, request?.HospitalArea);

                return StatusCode(500, new InpatientListResponse
                {
                    Success = false,
                    Message = "查询失败，请稍后重试",
                    Data = new List<InpatientInfo>(),
                    TotalCount = 0
                });
            }
        }

        /// <summary>
        /// 获取科室列表（用于前端下拉选择）
        /// </summary>
        /// <returns>科室列表</returns>
        [HttpPost("departments")]
        public IActionResult GetDepartments()
        {
            try
            {
                // 这里可以根据实际需求查询科室列表
                // 暂时返回示例数据，实际应该从数据库查询
                var departments = new List<object>
                {
                    new { Code = "1300110", Name = "内科一病区" },
                    new { Code = "1300120", Name = "内科二病区" },
                    new { Code = "1300210", Name = "外科一病区" },
                    new { Code = "1300220", Name = "外科二病区" }
                };

                _logger.LogInformation("科室列表查询成功，返回 {Count} 个科室", departments.Count);

                return Ok(new
                {
                    success = true,
                    message = "查询成功",
                    data = departments
                });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "科室列表查询失败");

                return StatusCode(500, new
                {
                    success = false,
                    message = "查询失败，请稍后重试",
                    data = default(object?)
                });
            }
        }
    }
}